﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Collections;
using System.Collections.Generic;
using Demo.Mmose.Core.Common.SafeCollections;
#endregion

namespace Demo.Mmose.Core.Entity.Suit.Instance
{

    /// <summary>
    /// 
    /// </summary>
    public class InstanceManager<InstanceT> : IEnumerable<InstanceT>
        where InstanceT : Instance
    {
        #region zh-CHS 共有方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, InstanceT> m_Instances = new SafeDictionary<long, InstanceT>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseTaxiNode"></param>
        public void Add( long instanceId, InstanceT instance )
        {
            if ( instance == null )
                throw new ArgumentNullException( "instance", "InstanceManager.Add(...) - instance == null error!" );

            m_Instances.Add( instanceId, instance );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public InstanceT FindInstanceOnId( long instanceId )
        {
            return m_Instances.GetValue( instanceId );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public void Remove( long instanceId )
        {
            m_Instances.Remove( instanceId );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <returns></returns>
        public bool Contains( long instanceId )
        {
            return m_Instances.ContainsKey( instanceId );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iQuestId"></param>
        public void Clear()
        {
            m_Instances.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        public InstanceT[] ToArray()
        {
            return m_Instances.ToArrayValues();
        }

        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<InstanceT> GetEnumerator()
        {
            InstanceT[] tempSpellArray = this.ToArray();
            if ( tempSpellArray == null )
                yield break;

            for ( int iIndex = 0; iIndex < tempSpellArray.Length; iIndex++ )
                yield return tempSpellArray[iIndex];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion
    }

}
#endregion
